package com.example.mybatis.mapper;

import com.example.mybatis.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
//由于mapper在xml里面已经被spring进行封装过，所以mapper就算没加spring也能使用spring的东西
public interface UserInfoMapper  {
    @Select("select * from user_info")
////由于列名对应不上，所以通过as改别名的办法来进行修改和类里面名字一样
//    @Select("select id,username,password,age,gender,phone,"+
//            " delete_flag as deleteFlag,create_time as createTime,update_time as updateTime"+
//            " from user_info;")

    //第二种利用Results的方法里面用@Result的方法改别名
//    @Results(id="BaseMap",value = {
//            @Result(column ="delete_flag",property ="deleteFlag" ),
//            @Result(column ="create_time",property ="createTime" ),
//            @Result(column ="update_time",property ="updateTime" )
//    })
//    @Select("select * from user_info")

    //第三种方法
    //在配置文件就直接修改东西，让这个自动变成驼峰写法
    List<UserInfo> selectAll();


//    @ResultMap(value ="BaseMap")
    @Select("select * from user_info where id= #{id}")
    UserInfo selsectAllById(Integer id);

    //可以用@Param修改名字
    @Select("select * from user_info where username=#{UserName} and password=#{password}")
    List<UserInfo> selectByNameAndPassword(@Param("UserName") String Username, @Param("password")String Password);

    //拿到自增id，在后端不用设置id都能自动自增,这样能获取id自增
    //前提是要对象
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into user_info(username,password,age) value(#{username},#{password},#{age})")
    Integer InsertUser(UserInfo userInfo);



    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into user_info(username,password,age) " +
            "value(#{userInfo.username},#{userInfo.password},#{userInfo.age})")
    Integer InsertUser2(@Param("userInfo") UserInfo userInfo);

    @Delete("delete from user_info where id=#{id}")
    Integer deleteUser(Integer id);

    @Update("update user_info set delete_flag=#{deleteFlag},phone=#{phone} where id=#{id}")
    Integer updateUser(UserInfo userInfo);
}
